package com.clothing.customization.service.impl;

import com.clothing.customization.common.PageResult;
import com.clothing.customization.common.exception.BusinessException;
import com.clothing.customization.entity.ClothingAccessory;
import com.clothing.customization.repository.ClothingAccessoryRepository;
import com.clothing.customization.service.ClothingAccessoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 服装配饰服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ClothingAccessoryServiceImpl implements ClothingAccessoryService {
    
    private final ClothingAccessoryRepository accessoryRepository;
    
    @Override
    public PageResult<ClothingAccessory> getAccessoryList(Integer pageNum, Integer pageSize, String name, String type, Integer status) {
        // 创建分页对象，按创建时间倒序排序
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        
        // 构建查询条件
        ClothingAccessory accessory = new ClothingAccessory();
        if (StringUtils.hasText(name)) {
            accessory.setName(name);
        }
        if (StringUtils.hasText(type)) {
            accessory.setType(type);
        }
        if (status != null) {
            accessory.setStatus(status);
        }
        
        // 创建匹配器，名称模糊匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact())
                .withIgnoreNullValues();
        
        // 查询
        Page<ClothingAccessory> page = accessoryRepository.findAll(Example.of(accessory, matcher), pageable);
        
        // 转换为自定义分页结果
        return new PageResult<>(page.getContent(), page.getTotalElements(), pageNum, pageSize);
    }
    
    @Override
    public ClothingAccessory getById(Long id) {
        return accessoryRepository.findById(id)
                .orElseThrow(() -> new BusinessException("配饰不存在"));
    }
    
    @Override
    @Transactional
    public ClothingAccessory addAccessory(ClothingAccessory accessory) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        accessory.setCreateTime(now);
        accessory.setUpdateTime(now);
        
        // 默认启用状态
        if (accessory.getStatus() == null) {
            accessory.setStatus(1);
        }
        
        return accessoryRepository.save(accessory);
    }
    
    @Override
    @Transactional
    public ClothingAccessory updateAccessory(ClothingAccessory accessory) {
        // 检查配饰是否存在
        ClothingAccessory existingAccessory = getById(accessory.getId());
        
        // 更新时间
        accessory.setUpdateTime(LocalDateTime.now());
        
        // 保留创建时间
        accessory.setCreateTime(existingAccessory.getCreateTime());
        
        return accessoryRepository.save(accessory);
    }
    
    @Override
    @Transactional
    public boolean deleteAccessory(Long id) {
        // 检查配饰是否存在
        ClothingAccessory accessory = getById(id);
        
        // 检查状态，只有禁用状态的配饰才能删除
        if (accessory.getStatus() == 1) {
            throw new BusinessException("启用状态的配饰不能删除，请先禁用");
        }
        
        accessoryRepository.deleteById(id);
        return true;
    }
    
    @Override
    @Transactional
    public boolean updateStatus(Long id, Integer status) {
        // 检查配饰是否存在
        ClothingAccessory accessory = getById(id);
        
        // 设置状态和更新时间
        accessory.setStatus(status);
        accessory.setUpdateTime(LocalDateTime.now());
        
        accessoryRepository.save(accessory);
        return true;
    }
} 